package com.itsccn.quartzdemo.scheduler;

import java.util.Date;

import javax.annotation.PostConstruct;

import static org.quartz.CronScheduleBuilder.cronSchedule;  
import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import com.itsccn.quartzdemo.Job.HelloJob;
public class CronTriggerExample {
	
	 public void run() throws Exception {  
		    Logger log = Logger.getLogger(getClass());
		    log.info("------- Initializing -------------------");  
		  
		    // 得到调度器的引用  
		    SchedulerFactory sf = new StdSchedulerFactory();  
		    Scheduler sched = sf.getScheduler();  
		  
		    log.info("------- Initialization Complete --------");  
		  
		    log.info("------- Scheduling Jobs ----------------");  
		  
		    // job可以在调度器运行前被调度  
		  
		    // job 1 每20秒运行1次  
		    JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group1").build();  
		    CronTrigger trigger =TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?")).build();  
		    Date ft = sched.scheduleJob(job, trigger);  
		    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "  
		             + trigger.getCronExpression());  
		  
		    // job 2 在偶数分钟的第15秒运行  
		    job =JobBuilder.newJob(HelloJob.class).withIdentity("job2", "group1").build();  
		    trigger =TriggerBuilder.newTrigger().withIdentity("trigger2", "group1").withSchedule(cronSchedule("15 0/2 * * * ?")).build();  
		    ft = sched.scheduleJob(job, trigger);  
		    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "  
		             + trigger.getCronExpression());  
		  
		    // job 3 在8-17点的偶数分钟运行  
		    job =JobBuilder.newJob(HelloJob.class).withIdentity("job3", "group1").build();  
		    trigger =TriggerBuilder.newTrigger().withIdentity("trigger3", "group1").withSchedule(cronSchedule("0 0/2 8-17 * * ?")).build();  
		    ft = sched.scheduleJob(job, trigger);  
		    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "  
		             + trigger.getCronExpression());  
		  
		    // job 4 在17-23点分钟是3的倍数运行  
		    job =JobBuilder.newJob(HelloJob.class).withIdentity("job4", "group1").build();  
		    trigger =TriggerBuilder.newTrigger().withIdentity("trigger4", "group1").withSchedule(cronSchedule("0 0/3 17-23 * * ?")).build();  
		    ft = sched.scheduleJob(job, trigger);  
		    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "  
		             + trigger.getCronExpression());  
		  
		    // job 5 在每月1号或15号的10点运行  
		    job =JobBuilder.newJob(HelloJob.class).withIdentity("job5", "group1").build();  
		    trigger =TriggerBuilder.newTrigger().withIdentity("trigger5", "group1").withSchedule(cronSchedule("0 0 10am 1,15 * ?")).build();  
		    ft = sched.scheduleJob(job, trigger);  
		    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "  
		             + trigger.getCronExpression());  
		  
		    // job 6 周一至周五的每30秒运行一次  
		    job =JobBuilder.newJob(HelloJob.class).withIdentity("job6", "group1").build();  
		    trigger =TriggerBuilder.newTrigger().withIdentity("trigger6", "group1").withSchedule(cronSchedule("0,30 * * ? * MON-FRI"))  
		        .build();  
		    ft = sched.scheduleJob(job, trigger);  
		    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "  
		             + trigger.getCronExpression());  
		  
		    // job 7 在周六、周日的每30秒运行一次  
		    job =JobBuilder.newJob(HelloJob.class).withIdentity("job7", "group1").build();  
		    trigger =TriggerBuilder.newTrigger().withIdentity("trigger7", "group1").withSchedule(cronSchedule("0,30 * * ? * SAT,SUN"))  
		        .build();  
		    ft = sched.scheduleJob(job, trigger);  
		    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "  
		             + trigger.getCronExpression());  
		  
		    log.info("------- Starting Scheduler ----------------");  
		  
		    // 调度器运行前所有任务不会运行  
		    sched.start();  
		  
		    log.info("------- Started Scheduler -----------------");  
		  
		    log.info("------- Waiting five minutes... ------------");  
		    try {  
		      Thread.sleep(300L * 1000L);  
		    } catch (Exception e) {  
		      e.printStackTrace();  
		    }  
		  
		    log.info("------- Shutting Down ---------------------");  
		  
		    sched.shutdown(true);  
		  
		    log.info("------- Shutdown Complete -----------------");  
		  
		    SchedulerMetaData metaData = sched.getMetaData();  
		    log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");  
		  
		  }  
}
